package com.smzd.ccms.business;

import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smzd.ccms.dao.CommentDao;
import com.smzd.ccms.domain.Comment;
import com.smzd.core.dao.IDao;
import com.smzd.framework.core.business.AbstractFGService;
import com.smzd.framework.system.domain.User;
import com.smzd.framework.utils.db.FGRowConverter;
import com.smzd.framework.utils.db.RPage;
import com.smzd.utils.SimpleDateTimeUtil;

@Service
public class CommentService extends AbstractFGService<Comment> {
	private static CommentService instance;
    @Autowired
    private CommentDao dao;
    
    @Override
    protected IDao<Comment> getDao() {
        return dao;
    }
    
    @Override
    public FGRowConverter<Comment> getConverter() {
        return converter;
    }

    private static FGRowConverter<Comment> converter = new FGRowConverter<Comment>() {
        @Override
        public Object[] convert(Comment elem) {
            Date posttime = elem.getPosttime();
            String str_posttime = null;
            if(posttime!=null){
                str_posttime = SimpleDateTimeUtil.formatToDateTime(posttime);
            }
            Date lastmodified = elem.getLastmodified();
            String str_lastmodified = null;
            if(lastmodified!=null){
                str_lastmodified = SimpleDateTimeUtil.formatToDateTime(lastmodified);
            }
            User user = elem.getUser();
            Object[] obj_user;
            if(user==null){
                obj_user = null;
            }else{
                Integer id_user = user.getId();
                String name_user = user.getName();
                String desc_user = user.getDescription();
                String avatar_user = user.getAvatar();
                obj_user = new Object[]{id_user, name_user, desc_user, avatar_user};
            }
            Object[] fs = new Object[]{
                    elem.getId(),
                    elem.getName(),
                    elem.getDescription(),
                    elem.getContent(),
                    elem.getSource(),
                    str_posttime,
                    str_lastmodified,
                    obj_user,
                    elem.getSticky(),
                    elem.getStatus(),
                    elem.getComment()==null?null:elem.getComment().getId()
                };
                return fs;
        }
    };

	@PostConstruct
	void init(){
		instance = this;
	}
	public static CommentService getInstance() {
		return instance;
	}

	/**
	 * 文章ID请放到page.id当中。
	 * @param page
	 * @return
	 */
    public RPage<Comment> getCommentsForArticle(RPage<Comment> page) {
        return dao.getCommentsForArticle(page);
    }

	public void updateCommentStatus(Comment comment) {
		dao.updateCommentStatus(comment);
	}

	/**
	 * 根据目录获取第一篇文章的评论。所谓的第一篇，其实是该目录底下ID最大的一篇。
	 * @param chId
	 * @return
	 */
	public List<Comment> getCommentsForFirstArticle(Integer chId) {
		return dao.getCommentsForFirstArticle(chId);
	}
}